﻿<%@ WebHandler Language="C#" Class="BinaryDataRetrival" %>

using System;
using System.Web;

public class BinaryDataRetrival : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        string tableName = context.Request.QueryString["tableName"];
        string rowId = context.Request.QueryString["rowId"];
        string columnName = context.Request.QueryString["columnName"];
        try
        {
            if (tableName != null && rowId != null && columnName != null)
            {
                Moddb08_WCF.Columns columns = frontEnd.GetAllColumns(tableName);
                foreach (Moddb08_WCF.Column col in columns)
                {
                    if (col.Name.Equals(columnName))
                    {
                        context.Response.ContentType = col.Mime;
                        byte[] data = frontEnd.GetColumnByteData(tableName, rowId, columnName);
                        if(data.Length != 0)
                        {
                            context.Response.BinaryWrite(data);
                            return;
                        }
                        else
                        {
                            context.Response.ContentType = "plain/text";
                            context.Response.Write("null");
                            return;
                        }
                    }
                }
            }
        }
        catch (System.ServiceModel.EndpointNotFoundException e)
        {
            context.Response.Write(e.InnerException.Message);
            return;
        }


        context.Response.ContentType = "text/plain";
        context.Response.Write("Wrong parameters sent to the handler");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}